package com.lagou.flashsale.listener;

import com.lagou.flashsale.dao.OrderDAO;
import com.lagou.flashsale.entity.Order;
import com.lagou.flashsale.entity.OrderStatus;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.support.RocketMQUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "tp_flashpay", consumerGroup = "grp_pay")
public class RocketMQPayListener implements RocketMQListener {

    @Autowired
    private OrderDAO orderDAO;

    @Autowired
    private RedisTemplate<String, Integer> redisTemplate;

    @Override
    public void onMessage(Object o) {
        Order order = new Order();
        order.setOrderId((String) o);
        // 若超时，设置为取消
        order.setOrderStatus(OrderStatus.CANCEL);
        boolean update = orderDAO.update(order);

        if (update) {
            redisTemplate.opsForValue().increment("goods_count");
            System.out.println("订单已取消。。。");
        }

        System.out.println(o + " 订单支付状态更改。。。");
    }
}
